<?php
session_start();
include '../conn.php';
include 'function.php';

$uid = $_REQUEST['uid'];

// 查询用户是否存在
$sql = "SELECT * FROM user WHERE id = '" . $uid . "'";
$result = $conn->query($sql);

if ($result->num_rows <= 0) {
    echo '请先注册或登录！';
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>商城</title>
  <link rel="stylesheet" type="text/css" href="../css/general.css">
  <link rel="stylesheet" type="text/css" href="../css/index.css">
  <link rel="stylesheet" type="text/css" href="../css/list.css">
  <script type="text/javascript" src="../js/jquery.js"></script>
  <script type="text/javascript" src="../js/general.js"></script>
  <script type="text/javascript" src="../js/carousel.js"></script>
  <style>
    .container {
      max-width: 1100px;
      margin: 0 auto;
      padding: 20px;
    }
    table {
      width: 100%;
      border-collapse: collapse;
    }
    th, td {
      border: 1px solid #ddd;
      padding: 8px;
    }
    th {
      background-color: #f2f2f2;
    }
    .empty-cart {
      text-align: center;
      font-size: 24px;
      color: #888;
      padding: 50px 0;
    }
  </style>
</head>
<body>
    <!-- 头部开始 -->
    <div class="header-body">
        <!-- 头部开始 -->
        <?php include 'header.php'; ?>
        <!-- 头部结束 -->
    </div>
    <!-- 头部结束 -->
    <!-- 主体开始 -->
    <div class="container w1100">
        <?php
        // 连接查询购物车数据
        $sql = "SELECT u.uname, g.goods_name, c.count, g.id AS goods_id 
                FROM cart c 
                JOIN user u ON c.user_id = u.id 
                JOIN goods g ON c.goods_id = g.id 
                WHERE c.user_id = " . $uid;
        $result = $conn->query($sql);

        // 检查查询是否成功
        if ($result === false) {
            die("查询失败: " . $conn->error);
        }

        // 打印查询结果，方便排查
        echo "<pre>";
        if ($result->num_rows > 0) {
            // print_r($result->fetch_all(MYSQLI_ASSOC));
        } else {
            echo "请前往首页添加商品";
        }
        echo "</pre>";

        if ($result->num_rows > 0) {
            echo "<table style='width:100%;'>";
            echo "<tr>";
            echo "<td colspan='6' style='text-align:center;'>我的购物车</td>";
            echo "</tr>";
            echo "<tr>";
            echo "<th>用户名</th><th>商品名称</th><th>数量</th><th>修改操作</th><th>删除操作</th><th>付款操作</th>";
            echo "</tr>";
            $result->data_seek(0); // 重置结果指针
            while ($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>" . htmlspecialchars($row['uname']) . "</td>";
                echo "<td>" . htmlspecialchars($row['goods_name']) . "</td>";
                echo "<td>" . htmlspecialchars($row['count']) . "</td>";
                echo "<td><a href='editcart.php?uid=" . htmlspecialchars($uid) . "&gid=" . htmlspecialchars($row['goods_id']) . "&count=" . htmlspecialchars($row['count']) . "'>更改</a></td>";
                echo "<td><a href='deletecart.php?uid=" . htmlspecialchars($uid) . "&gid=" . htmlspecialchars($row['goods_id']) . "'>删除</a></td>";
                echo "<td><a href='pay.php?uid=" . htmlspecialchars($uid) . "&gid=" . htmlspecialchars($row['goods_id']) . "&count=" . htmlspecialchars($row['count']) . "'>付款</a></td>";
                echo "</tr>";
            }
            echo "</table>";
        } else {
            echo "<div class='empty-cart'>购物车为空</div>";
        }
        ?>
        <p style='font-size:30px'><a href="payall.php?uid=<?php echo htmlspecialchars($uid); ?>">全部付款</a></p>
        <p style='font-size:30px'><a href="cleancart.php?uid=<?php echo htmlspecialchars($uid); ?>">清空购物车</a></p>
    </div>
    <!-- 主体结束 -->
    <!-- 页脚开始 -->
    <div class="footer">
        <?php include '../footer.php'; ?>
    </div>
    <!-- 页脚结束 -->
</body>
</html>